 package com.todomvc.fw;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.util.ArrayList;
import java.util.List;

public class TodoHelper extends HelperWithWebDriverBase {
	
	public TodoHelper(ApplicationManager applicationManager) {
		super(applicationManager);
	}
	
	public void add (Todo todo){
		type(By.id("new-todo"), todo.getText() + "\n");
	}
	
	public boolean getToggleAllCheckboxState () {
		return findElement(By.id("toggle-all")).isSelected();
	}
	
	public void toggleAll () {
		click(By.id("toggle-all"));
	}
	
	public String getTodoListText () {
		return findElement(By.id("todo-list")).getText();
	}

	public boolean todoListIsNotEmpty () {
		return !getTodoListText().isEmpty();
	}
	
	
	public List<WebElement> getListElements () {
		return findElements(By.cssSelector("ul#todo-list li"));
	}

	
	public int getListSize () {
		if (todoListIsNotEmpty ()) {
			return getListElements().size();
		}
		return 0;
	}	

	
	public WebElement getListElement (int index) {
		return findElement(By.cssSelector("ul#todo-list li:nth-child(" + index + ")"));
	}

	
	public WebElement getFirstListElement () {
		return findElement(By.cssSelector("ul#todo-list li:first-child"));
	}

	
	public WebElement getLastListElement () {
		return findElement(By.cssSelector("ul#todo-list li:last-child"));
	}

	
	public WebElement getIsCompletedCbx (WebElement listElement) {
		return listElement.findElement(By.cssSelector("div.view input[type=checkbox]"));
	}

	
	public WebElement getLbl (WebElement listElement) {
		return listElement.findElement(By.cssSelector("div.view label"));
	}

	
	public WebElement getDestroyButtonElement (WebElement listElement) {
		return listElement.findElement(By.cssSelector("div.view button.destroy"));
	}

	
	public Todos getTodos () {
		List<Todo> todos = new ArrayList<Todo>();
		if (todoListIsNotEmpty()){
			List<WebElement> listElements = getListElements();
			for (WebElement listElement : listElements) {
				todos.add(new Todo (getIsCompletedCbx(listElement).isSelected(), getLbl(listElement).getText()));
			}
		}
		return new Todos(todos);
	}

	
	//public List<WebElement> getToggleCheckboxList () {
	//		return findElements(By.cssSelector("div.view input[type=checkbox]"));
	//}

	//public int getToggleCheckboxListSize () {
	//	if (todoListIsNotEmpty ()) {
	//		return getToggleCheckboxList().size();
	//	}
	//	return 0;
	//}	

	public boolean getNthToggleCheckboxState (int index) {
		return findElement(By.cssSelector("ul#todo-list li:nth-child(" + index + ") div.view input[type=checkbox]")).isSelected();
	}	

	public void toggleIsCompleted (WebElement listElement) {
		getIsCompletedCbx(listElement).click();
	}

	
	public void toggleFirst () {
			getIsCompletedCbx(getFirstListElement()).click();
	}

	
	public void toggleLast () {
			getIsCompletedCbx(getLastListElement()).click();
	}
 
	
	public void toggleNth (int index) {
		if (todoListIsNotEmpty()) {
			getIsCompletedCbx(getListElement(index)).click();
		}
	}
	
	//public List<WebElement> getDestroyButtonList () {
	//	return findElements(By.cssSelector("div.view button.destroy"));
	//}

	public void destroy (WebElement listElement) {
		moveTo(getLbl(listElement));
		getDestroyButtonElement(listElement).click();
	}
	
	public void destroyFirst () {
		destroy(getFirstListElement());
	}

	public void destroyLast () {
		destroy(getLastListElement());
	}

	public void destroyNth (int index) {
		if (todoListIsNotEmpty() && (index > 0) && (index <= getListSize())) {
			destroy(getListElement(index));
		}	
	}
	
	public String getCompletedCount () {
		return findElement(By.id("clear-completed")).getText();
	}

	public int getTodoCount () {
		return Integer.parseInt(findElement(By.cssSelector("span#todo-count strong")).getText());
	}
	
	public String getFilterState () {
		return findElement(By.cssSelector("ul#filters li a.selected")).getText();
	}

	public void selectFilterAll () {
		click(By.cssSelector("ul#filters li:nth-child(1) a"));
	}

	public void selectFilterActive () {
		click(By.cssSelector("ul#filters li:nth-child(2) a"));
	}

	public void selectFilterCompleted () {
		click(By.cssSelector("ul#filters li:nth-child(3) a"));
	}

	public void clearCompleted () {
		click(By.id("clear-completed"));
	}
}
